package com.rjpacket.supernews.app;

import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.rjpacket.supernews.app.model.DuanziDetail;
import com.rjpacket.supernews.app.utils.HttpTool;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 段子详情的Activity
 */
public class DuanziDetailActivity extends ActionBarActivity implements View.OnClickListener {

    private TextView txt_content;
    private Button btnCheckOrigin;

    private ImageView imgZan;
    private ImageView imgComment;
    private ImageView imgSave;
    private ImageView imgTransfer;

    //判断点赞是否已经被点击
    private boolean isPressed = false;
    //判断是否收藏
    private boolean isSaved = false;
    //用于存储传递过来的原始网址
    private String original_url;
    private String share_url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mz_activity_duanzi_detail);

        txt_content = (TextView) findViewById(R.id.activity_duanzi_detail_content);
        btnCheckOrigin = (Button) findViewById(R.id.btn_activity_check_origin);

        //最底部的ImageView
        imgComment = (ImageView) findViewById(R.id.activity_detail_img_comment);
        imgZan = (ImageView) findViewById(R.id.activity_detail_img_zan);
        imgTransfer = (ImageView) findViewById(R.id.activity_detail_img_transfer);
        imgSave = (ImageView) findViewById(R.id.activity_detail_img_save);

        //获得传递过来的参数
        Intent intent = getIntent();
        DuanziDetail detail = (DuanziDetail) intent.getSerializableExtra("news");
        original_url = detail.getOriginal_url();
        share_url = detail.getShare_url();
        String content = intent.getStringExtra("content");

        //设置内容
        txt_content.setText(content);

        //设置点击事件
        btnCheckOrigin.setOnClickListener(this);
        imgComment.setOnClickListener(this);
        imgSave.setOnClickListener(this);
        imgZan.setOnClickListener(this);
        imgTransfer.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_activity_check_origin:
                //浏览原文网址
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(original_url));
                startActivity(intent);
                break;
            case R.id.activity_detail_img_comment:
                //跳转到评论界面
                Intent intent_comment = new Intent(DuanziDetailActivity.this, CommentActivity.class);
                startActivity(intent_comment);
                break;
            case R.id.activity_detail_img_zan:

                if (!isPressed) {
                    //设置成红色的赞
                    imgZan.setImageResource(R.drawable.ic_have_good);
                    //TODO  显示动画
                    isPressed = true;
                } else {
                    Toast.makeText(DuanziDetailActivity.this, "已经赞过", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.activity_detail_img_save:
                if (!isSaved) {
                    imgSave.setImageResource(R.drawable.ic_newsbar_collected);
                    Toast.makeText(DuanziDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    isSaved = true;
                } else {
                    imgSave.setImageResource(R.drawable.ic_save);
                    Toast.makeText(DuanziDetailActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.activity_detail_img_transfer:
                //TODO 分享
                break;
        }

    }
}
